package main

import "fmt"

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

//计算第s行e列是否是边长为b的正方形
func isSquareB(matrix [][]byte, s, e, b int) bool {
	l := len(matrix)
	if l == 0 {
		return false
	}
	m := len(matrix[0])
	if s+b > l || e+b > m {
		return false
	}
	for i := s; i < s+b; i++ {
		for j := e; j < e+b; j++ {
			if matrix[i][j] == 0 {
				return false
			}
		}
	}
	return true
}

func maximalSquare(matrix [][]byte) int {
	var maxB = 0
	var l = len(matrix)
	if l == 0 {
		return 0
	}
	var m = len(matrix[0])
	var maymaxB = min(l, m)
	for i := 0; i < l-maxB; i++ {
		for j := 0; j < m-maxB; j++ {
			n := maxB + 1
			for n <= maymaxB {
				if isSquareB(matrix, i, j, n) {
					maxB = n
					n++
					continue
					//break
				}
			}
			fmt.Println(i, j, n)
		}
	}
	return maxB * maxB
}

func main() {
	matrix := [][]byte{
		[]byte{1, 0, 1, 0, 0},
		[]byte{1, 0, 1, 1, 1},
		[]byte{1, 1, 1, 1, 1},
		[]byte{1, 0, 0, 1, 1},
		//[]byte{1, 0, 1, 0, 1,0},
	}
	//matrix := [][]byte{[]byte{1}}
	maxS := maximalSquare(matrix)
	fmt.Println(maxS)
	a := isSquareB(matrix, 1, 6, 3)
	fmt.Println(a)
}
